﻿#include "FITKAbaqusAdaptorConnectorStopBehavior.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "InpReadInformationMapper.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractAssInstance.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConnectorSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConnectorBehaviorStop.h"
#include <QDebug>
//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorConnectorStopBehavior::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorConnectorStopBehavior";
    }

    bool FITKAbaqusAdaptorConnectorStopBehavior::adaptR()
    {
        _sectionConnectorData = dynamic_cast<Interface::FITKAbaConnectorSection*>(_dataObj);
        if (!_sectionConnectorData || !_reader)return false;
        //回退一行，处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*connector stop")) return false;
        //添加行为
        _stopBehaviorData = dynamic_cast<Interface::FITKAbaConnectorBehaviorStop*>
            (_sectionConnectorData->addBehavior(Interface::FITKAbaConnectorBehaviorEnum::ConnectorBehaviorType::Stop));
        if (!_stopBehaviorData)return false;
        bool readOK = false;
        InpKeyLineSpliter s(line, _reader);
        //力矩类型
        this->_component = s["component"].trimmed().toInt(&readOK);
        if (!readOK)return false;
        if (_component < 1 || _component > 6)return false;
        std::array<bool, 6> componentsState{ false, false, false, false, false, false };
        componentsState[_component - 1] = true;
        _stopBehaviorData->setComponents(componentsState);
        //读取stop的数据
        readOK = this->readStopData();
        //读取失败就删除
        if (!readOK)
            _sectionConnectorData->removeBehavior(_stopBehaviorData);
        return readOK;
    }

    bool FITKAbaqusAdaptorConnectorStopBehavior::adaptW()
    {
        _sectionConnectorData = dynamic_cast<Interface::FITKAbaConnectorSection*>(_dataObj);
        if (!_sectionConnectorData || !_stopBehaviorData || !_writer)return false;
        bool writeOK = false;
        QTextStream* stream = _writer->getStream();
        std::array<bool, 6> componentsState = _stopBehaviorData->getComponents();
        for (int i = 0; i < 6; ++i)
        {
            if (!componentsState[i])continue;
            *stream << QString("*Connector Stop, component=%1").arg(i + 1) << endl;
            writeOK = this->writeStopBehavior();
            if (!writeOK)return false;
        }
        return true;
    }

    void FITKAbaqusAdaptorConnectorStopBehavior::setWriteBehaviorData(Interface::FITKAbaConnectorBehaviorStop* stopData)
    {
        _stopBehaviorData = stopData;
    }

    bool FITKAbaqusAdaptorConnectorStopBehavior::readStopData()
    {
        if (!_stopBehaviorData || !_reader)return false;
        QString line = _reader->readLine().trimmed();
        QStringList str = line.split(',');
        if (str.size() != 2)return false;
        bool ok = false;
        _stopBehaviorData->setUseLowerBound(false);
        _stopBehaviorData->setUseUpperBound(false);
        //获取下边界
        double lowerBound = 0.0;
        if (!str.at(0).trimmed().isEmpty())
        {
            lowerBound = str.at(0).trimmed().toDouble(&ok);
            if (!ok)return false;
            _stopBehaviorData->setUseLowerBound(true);
        }
        //获取上边界
        double upperBound = 0.0;
        if (!str.at(1).trimmed().isEmpty())
        {
            upperBound = str.at(1).trimmed().toDouble(&ok);
            if (!ok)return false;
            _stopBehaviorData->setUseUpperBound(true);
        }
        if (upperBound <= lowerBound)return false;
        //设置边界
        _stopBehaviorData->setLowerBound(lowerBound);
        _stopBehaviorData->setUpperBound(upperBound);
        //再次读取一行
        _reader->readLine();
        return true;
    }

    bool FITKAbaqusAdaptorConnectorStopBehavior::writeStopBehavior()
    {
        if (!_writer || !_stopBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        QString lowerBound, upperBound;
        //下边界
        if (_stopBehaviorData->getUseLowerBound())
            lowerBound = _writer->double2String(_stopBehaviorData->getLowerBound(), 8, 0);
        //上边界
        if (_stopBehaviorData->getUseUpperBound())
            upperBound = _writer->double2String(_stopBehaviorData->getUpperBound(), 8, 0);
        *stream << QString("%1, %2").arg(lowerBound).arg(upperBound) << endl;
        return true;
    }
}